package com.zisuyi.game.sdk.base

import androidx.lifecycle.viewModelScope
import com.weilele.mvvm.base.MvvmViewModel
import com.weilele.mvvm.base.helper.*
import com.zisuyi.game.sdk.base.net.Api
import com.zisuyi.game.sdk.base.net.ApiInstance
import com.zisuyi.game.sdk.base.net.bindLiveDataWithNetwork
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

abstract class BaseViewModel : MvvmViewModel() {
    /**
     * 访问网络请求
     */
    fun <T> StatusLiveData<T>.bindNetwork(
            context: CoroutineContext = EmptyCoroutineContext,
            start: CoroutineStart = CoroutineStart.DEFAULT,
            resultCall: LiveDataRunnable = DefaultLiveDataRunnable(),
            isToastError: Boolean = true/*是否toast出错误*/,
            isKeepLastValue: Boolean = false/*是否保留之前已经赋值的数据*/,
            errorCall: ((Throwable) -> T?)? = null,
            call: suspend Api.() -> T?): Job {
        return viewModelScope.bindLiveDataWithNetwork(
                ApiInstance,
                this,
                context,
                start,
                resultCall,
                isToastError,
                isKeepLastValue,
                errorCall,
                call)
    }
}